package com.anbinh.doctor.adapter;

import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.anbinh.doctor.R;
import com.anbinh.doctor.model.AppointmentResultSearch.Data.Item;
import com.anbinh.doctor.utils.Utils;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.assist.SimpleImageLoadingListener;

public class SearchLichhenAdapter extends ArrayAdapter<Item> {
	private int resource;
	private LayoutInflater layoutInflater;
	private List<Item> listData;
	private ImageLoader imageLoader;
	private Context mContext;

	public SearchLichhenAdapter(Context mContext, int resource,
			List<Item> models) {
		super(mContext, resource, models);
		this.resource = resource;
		this.listData = models;
		this.mContext = mContext;
		layoutInflater = LayoutInflater.from(mContext);
		imageLoader = ImageLoader.getInstance();
		imageLoader.init(ImageLoaderConfiguration.createDefault(mContext
				.getApplicationContext()));
	}

	public class ViewHolder {
		public TextView lblDoctorName;
		public TextView stt;
		public TextView lblChuyenkhoa;
		public TextView lblLocationPhone;
		public ImageView icon;

	}

	@Override
	public Item getItem(int position) {
		return listData.get(position);
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder;
		if (convertView == null) {
			viewHolder = new ViewHolder();
			convertView = layoutInflater.inflate(resource, null, true);

			viewHolder.lblDoctorName = (TextView) convertView
					.findViewById(R.id.lblDoctorName);

			viewHolder.lblChuyenkhoa = (TextView) convertView
					.findViewById(R.id.lblChuyenkhoa);

			viewHolder.lblLocationPhone = (TextView) convertView
					.findViewById(R.id.lblLocationPhone);
			viewHolder.icon = (ImageView) convertView.findViewById(R.id.icon);
			convertView.setTag(viewHolder);

		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		Item item = getItem(position);
		viewHolder.lblDoctorName.setText(item.getNamePatient());
		viewHolder.lblChuyenkhoa.setText(item.getNameStatus());
		viewHolder.lblLocationPhone.setText("Thời gian khám: "+ item.getBlock_time()+"-" + item.getDate());

		Bitmap bmp = imageLoader.loadImageSync(item.getAvatar());
		if (bmp == null) {

			imageLoader.displayImage(item.getAvatar(), viewHolder.icon,
					Utils.getOptions(mContext),
					new SimpleImageLoadingListener() {
						@Override
						public void onLoadingStarted(String imageUri, View view) {
						}

						@Override
						public void onLoadingFailed(String imageUri, View view,
								FailReason failReason) {
						}

						@Override
						public void onLoadingComplete(String imageUri,
								View view, Bitmap loadedImage) {
						}
					});
		} else {
			viewHolder.icon.setImageBitmap(bmp);
		}

		return convertView;
	}
}
